﻿using System;
using System.Collections.Generic;

namespace Ledict.Utility.Extension
{
    /// <summary>
    /// 时间的多种扩展操作   
    /// </summary>
    /// <remarks>
    /// Create Date:2014-05-28
    /// Create Author:Zhangpeng
    /// </remarks>
    public static class DateTimeExtensions
    {
        /// <summary>
        /// 获取给定时间的最后时间
        /// </summary>
        /// <param name="src">给点时间</param>
        /// <returns>给定时间的最后时间</returns>
        public static DateTime EndOfDay(this DateTime src)
        {
            return new DateTime(src.Year, src.Month, src.Day, 23, 59, 59);
        }

        /// <summary>
        /// 获取给定时间的最小时间
        /// </summary>
        /// <param name="src">给定时间</param>
        /// <returns>给定时间的最小时间</returns>
        public static DateTime BeginOfDay(this DateTime src)
        {
            return new DateTime(src.Year, src.Month, src.Day, 0, 0, 0);
        }

        /// <summary>
        /// 获取给定时间当前月的第一天
        /// </summary>
        /// <param name="src">给定时间</param>
        /// <returns>给定时间当前月的第一天</returns>
        public static DateTime BeginOfMonth(this DateTime src)
        {
            return new DateTime(src.Year, src.Month, 1, 0, 0, 0);
        }

        /// <summary>
        /// 获取给定时间当前月的最后一天
        /// </summary>
        /// <param name="src">给定时间</param>
        /// <returns>给定时间当前月的最后一天</returns>
        public static DateTime EndOfMonth(this DateTime src)
        {
            return src.BeginOfMonth().AddMonths(1).AddSeconds(-1);
        }

        /// <summary>
        /// 获取给定时间上个月的最后一天
        /// </summary>
        /// <param name="src">给定时间</param>
        /// <returns>给定时间上个月的最后一天</returns>
        public static DateTime LastMonthEnd(this DateTime src)
        {
            return src.BeginOfMonth().AddSeconds(-1);
        }

        /// <summary>
        /// 获取给定时间上个月的第一天
        /// </summary>
        /// <param name="src">给定时间</param>
        /// <returns>给定时间上个月的第一天</returns>
        public static DateTime LastMonthBegin(this DateTime src)
        {
            return src.LastMonthEnd().BeginOfMonth();
        }

        private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, 0);
        /// <summary>
        /// DateTime转换为unixTime时间
        /// </summary>
        /// <param name="time">DateTime</param>
        /// <returns>unixTime时间</returns>
        public static long ToUnixTime(this DateTime time)
        {
            return (long)(time.ToUniversalTime() - UnixEpoch).TotalSeconds;
        }

        /// <summary>
        /// unixTime时间转换为DateTime类型
        /// </summary>
        /// <param name="unixTime">unixTime时间</param>
        /// <returns>DateTime类型</returns>
        public static DateTime FromUnixTime(this long unixTime)
        {
            if (unixTime < 0)
                throw new ArgumentOutOfRangeException("unixTime");

            return UnixEpoch.AddSeconds(unixTime);
        }

        public static string ToCnString(this TimeSpan ts)
        {
            string r = string.Empty;
            if (ts.Days > 0)
            {
                r += ts.Days + "天";
            }
            if (ts.Hours > 0)
            {
                r += ts.Hours + "小时";
            }
            else if (ts.Days > 0 && ts.Minutes > 0)
            {
                r += "零";
            }
            if (ts.Minutes > 0)
            {
                r += ts.Minutes + "分";
            }
            if (string.IsNullOrEmpty(r))
            {
                if (ts.Seconds > 0)
                {
                    r = ts.Seconds + "秒";
                }
                else
                {
                    r = "0分";
                }
            }
            return r;
        }

        /// <summary>
        /// 根据给定年份，获取这一年给定起始月之后的所有日期列表  add by zp
        /// </summary>
        /// <param name="year">给定年份</param>
        /// <returns>时间类</returns>
        public static IEnumerable<PeroidDateTime> GetDateTimeList(int year)
        {
            var list = new List<PeroidDateTime>();


            for (int i = 1; i <= 12; i++)
            {
                var model = new PeroidDateTime();
                if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
                {
                    model.StartDate = new DateTime(year, i, 1);
                    model.EndDate = new DateTime(year, i, 31);

                }
                else if (i == 2)
                {
                    //闰年
                    if (year % 4 == 0 || (year % 100 == 0 && year % 400 == 0))
                    {
                        model.StartDate = new DateTime(year, i, 1);
                        model.EndDate = new DateTime(year, i, 29);
                    }
                    //平年
                    else
                    {
                        model.StartDate = new DateTime(year, i, 1);
                        model.EndDate = new DateTime(year, i, 28);
                    }

                }
                else
                {
                    model.StartDate = new DateTime(year, i, 1);
                    model.EndDate = new DateTime(year, i, 30);
                }

                list.Add(model);

            }
            return list;

        }
    }

    #region 时间类
    /// <summary>
    /// 描述：时间类
    /// 时间：2012-12-14
    /// 作者：张鹏
    /// </summary>
    public class PeroidDateTime
    {
        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime StartDate { get; set; }
        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }
    }
    #endregion
}